package com.app.brilliant.game.service.impl;
import com.app.brilliant.common.service.impl.BaseServiceImpl;
import com.app.brilliant.game.bean.BriClassification;
import com.app.brilliant.game.mapper.WxUserMapper;
import com.app.brilliant.game.response.BriClassificationCourseInfo;
import com.app.brilliant.game.bean.BriCourse;
import com.app.brilliant.game.response.BriClassificationCourse;
import com.app.brilliant.game.service.BriClassificationService;
import com.app.brilliant.game.mapper.BriClassificationMapper;
import com.app.brilliant.game.service.WxConfigService;
import com.app.brilliant.game.vo.CourseProgressVO;
import com.app.brilliant.sign.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author GF
* @description 针对表【bri_classification】的数据库操作Service实现
* @createDate 2025-09-01 20:44:27
*/
@Service
public class BriClassificationServiceImpl extends BaseServiceImpl<BriClassificationMapper, BriClassification>
implements BriClassificationService{
    @Autowired
    private WxUserMapper wxUserMapper;
    @Autowired
    private WxConfigService wxConfigService;
    @Override
    public List<BriClassificationCourseInfo> selectClassificationWithCourses(Integer userId) {
        List<BriClassificationCourse> list = baseMapper.selectClassificationWithCourses();
        List<CourseProgressVO> courseProgressVOList = wxUserMapper.selectCourseProgressByUserId(userId);
        Map<Integer, List<BriClassificationCourse>> collect = list.stream().collect(Collectors.groupingBy(BriClassificationCourse::getId));
        List<BriClassificationCourseInfo> briClassificationCourseInfos = new ArrayList<>();
        String domain = wxConfigService.getConfigValue(Constants.NomalConfigKey.PictureDomain);
        collect.forEach((key,value)->{
            if(!value.isEmpty()){
                BriClassificationCourse first = value.get(0);
                BriClassificationCourseInfo briClassificationCourseInfo = new BriClassificationCourseInfo();
                BriClassification briClassification = new BriClassification();
                briClassification.setClassificationName(first.getClassificationName());
                briClassification.setDesc(first.getDesc());
                briClassification.setIcon(domain+first.getIcon());
                briClassification.setId(first.getId());
                briClassificationCourseInfo.setClassification(briClassification);
                List<BriClassificationCourseInfo.BriCourseInfo> courses = value.stream().map(v -> {
                    BriClassificationCourseInfo.BriCourseInfo briCourse = new BriClassificationCourseInfo.BriCourseInfo();
                    briCourse.setId(v.getCourseId());
                    briCourse.setCourseName(v.getCourseName());
                    briCourse.setIcon(domain+v.getBcIcon());
                    briCourse.setDesc(v.getBcDesc());
                    courseProgressVOList.stream().filter(q->q.getCourseId().equals(v.getCourseId())).findFirst().ifPresent(q->{
                        briCourse.setComplete(q.getCompletedHours());
                        briCourse.setTotal(q.getTotalHours());
                    });
                    return briCourse;
                }).collect(Collectors.toList());
                briClassificationCourseInfo.setCourses(courses);
                briClassificationCourseInfos.add(briClassificationCourseInfo);
            }
        });
        return briClassificationCourseInfos;
    }
}
